package by.grsu.samusev.page.pages;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.samusev.app.SamusevWebApplication;
import by.grsu.samusev.dao.impl.DriverDao;
import by.grsu.samusev.entity.Driver;

public class DriverEditPage extends WebPage {
	public DriverEditPage(final Driver driver) {
		super();
		

		final Form<Driver> form = new Form<Driver>("form", new CompoundPropertyModel<Driver>(driver));
		add(form);
		

		// add input fields to form
		form.add(new TextField<String>("name"));
		
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				DriverDao driverDao = new DriverDao(SamusevWebApplication.DATA_XML_FOLDER);
				if (driver.getId() == null) {
					System.out.println("save="+driver);
					driverDao.saveNew(driver);
				} else {
					System.out.println("update="+driver);
					driverDao.update(driver);
				}

				setResponsePage(new DriverListPage());
			}
		});
		
		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new DriverListPage());
			}
		});
		
	}

}
